package com.ruoyi.datastatistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.basedata.domain.UsersXb;
import com.ruoyi.basedata.mapper.UsersXbMapper;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.datastatistics.domain.xbmaindata.*;
import com.ruoyi.datastatistics.mapper.MainXbMapper;
import com.ruoyi.datastatistics.service.IMainXbService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class MainXbServiceImpl implements IMainXbService {
    @Autowired
    private MainXbMapper mainXbMapper;

    @Autowired
    private UsersXbMapper usersXbMapper;

    @Override
    public Map<String, Object> getMainHeadDataDb() {
        Map<String,Object> headerMap = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<MainMeterNumData> mainMeterNumData = mainXbMapper.countTerminalNum();
        Integer notBindingNum = mainXbMapper.countNotBinding();
        Integer bindingNum = mainXbMapper.countBinding();
        Integer todayAddUserNum = mainXbMapper.addUserNum(DateUtil.getStartTime(new Date()),new Date());
        Integer thisMonthAddUserNum = mainXbMapper.addUserNum(DateUtil.getMonthStart(), new Date());
        MainMeterHeaderReadingRate meterReadingRate = mainXbMapper.meterReadingRate(DateUtil.getLastDayStartDate(sdf.format(new Date())), sdf.format(new Date()));
        Long userNum  = usersXbMapper.selectCount(new LambdaQueryWrapper<UsersXb>()
                .eq(UsersXb::getDelFlag,0)
                .eq(UsersXb::getUserMark,1));
        Integer todayWarnNum = mainXbMapper.getWarnNum(DateUtil.getStartTime(new Date()), new Date());
        headerMap.put("terminalSupplierNameNum",mainMeterNumData);
        headerMap.put("notBindingNum",notBindingNum);
        headerMap.put("bindingNum",bindingNum);
        headerMap.put("todayAddUserNum",todayAddUserNum);
        headerMap.put("thisMonthAddUserNum",thisMonthAddUserNum);
        headerMap.put("meterReadingRate",meterReadingRate);
        headerMap.put("terminalNum",mainMeterNumData.stream().mapToInt(MainMeterNumData::getNum).reduce(0,Integer::sum));
        headerMap.put("userNum",userNum);
        headerMap.put("todayWarnNum",todayWarnNum);
        return headerMap;
    }

    @Override
    public List<MainMeterWarnData> getMainWarnDataXb() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return mainXbMapper.warnData(DateUtil.getLastDayStartDate(sdf.format(new Date())),DateUtil.getLastDayEndDate(sdf.format(new Date())));
    }

    @Override
    public List<MainMeterReadingRate> getMeterReadingRate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return mainXbMapper.getMeterReadingRate(DateUtil.getLastDayStartDate(sdf.format(new Date())),DateUtil.getLastDayEndDate(sdf.format(new Date())));
    }

    @Override
    public List<MainMeterGrowthRate> getGrowthRate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<MainMeterGrowthRate> growthRateList = mainXbMapper.getGrowthRate(sdf.format(DateUtil.getMonthStart()),
                sdf.format(new Date()),
                DateUtil.getLastMonthFirstDate(sdf.format(new Date())),
                DateUtil.getLastMonthEndDate(sdf.format(new Date())));
        NumberFormat nt = NumberFormat.getPercentInstance();
        nt.setMinimumFractionDigits(2);
        growthRateList.forEach(growthRate->{
            if (growthRate.getLastMonthCumulativeFlow().compareTo(BigDecimal.ZERO) == 0){
                growthRate.setGrowthRate(nt.format(BigDecimal.ONE.doubleValue()));
            }else {
                growthRate.setGrowthRate(nt.format((growthRate.getThisMonthCumulativeFlow().doubleValue() - growthRate.getLastMonthCumulativeFlow().doubleValue())/growthRate.getLastMonthCumulativeFlow().doubleValue()));
            }
        });
        return growthRateList;
    }
}
